
package org.teama;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;

/**
 *
 * @author xiopus
 */
@WebServlet(name = "getIssueList", urlPatterns = {"/getIssueList"})
public class getIssueList extends HttpServlet {
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurst
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        RequestDispatcher dispatcher;

        /*Force reload in case there are new issues */
        if((IssueListDA.load()) != false){
        
        /*Get the issue list sorted according to user's preference. */
        User activeUser = (User) request.getSession().getAttribute(SessionAttributes.getActiveUserName());
        int sortPref = activeUser.getSortPreference();
        ArrayList<Issue> il = IssueListDA.getIssueList(sortPref);

        request.setAttribute("issueList", il);
 
        //Forward to JSP
        dispatcher = getServletConfig().getServletContext().getRequestDispatcher("/displayIssues.jsp");
        dispatcher.forward(request,response);        
        }else{
            request.setAttribute("error", "Database is not available.");
            dispatcher = getServletConfig().getServletContext().getRequestDispatcher("/error.jsp");
            dispatcher.forward(request,response);
        }
        
    }    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Read issues and create list";
    }// </editor-fold>

}
